package com.dao.batch.service;

import com.dao.batch.controller.UserController;
import com.dao.batch.entity.User;
import com.dao.batch.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/9/27.
 */
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    private final static Logger logger = LoggerFactory.getLogger(UserService.class);
    /**
     * 查询所有user信息列列表
     * @return
     */
    public List<User> findAll() {
        logger.info("chaxunxunxunx-----------------");
        return userRepository.findAll();
    }
    /**
     * 分⻚页查询user信息，且使⽤用默认排序
     * @return
     */
    public Page<User> findByPage(int page, int pageSize) {
        Pageable userPage = new PageRequest(page, pageSize);
        return userRepository.findAll(userPage);
    }
    /**
     * 分⻚页查询user信息，⾃自定义排序
     * @return
     */
    public Page<User> findByPage(int page, int pageSize, Sort sort) {
        Pageable userPage = new PageRequest(page, pageSize,sort);
        return userRepository.findAll(userPage);
    }
    /**
     * ⽆无主键值，插⼊入到库
     *
     * @param user
     * @return
     */
    public User addUser(User user) {
        return userRepository.save(user);
    }
    /**
     * 根据主键id值更更新
     *
     * @param user
     * @return
     */
    public User updateUser(User user) {
        return userRepository.save(user);
    }
    /**
     * 根据主键id查询⽤用户信息
     *
     * @param id
     * @return
     */
    public User findUser(Integer id) {
        return userRepository.findOne(id);
    }
    /**
     * 根据主键id删除⽤用户信息
     *
     * @param id
     */
    public void deleteUser(Integer id) {
        userRepository.delete(id);
    }
}
